using System;
using System.Collections.Generic;
using System.Text;
using MVCFramework.Models;


namespace MVCFramework.Presenter.Commands
{
    /// <summary>
    /// this a Generic class for adding elemets to modelcontroller .
    /// this is permited since  modelcontroller addObject covers every 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    internal class AddObjectCommand<T> : Command where T : class, IModelData  
    {
        private IModel _model;
        private IList<T> _objects;

        public AddObjectCommand(IModel constraction, IList<T> elements)
        {
            this._model = constraction;
            this._objects = elements;
        }

        public AddObjectCommand(IModel constraction, T element)
        {
            this._model = constraction;
            this._objects = new List<T>();
            this._objects.Add ( element);
        }

        public override bool Execute()
        {
            bool AreAdded = false;

            List<T> AddedObjects = new List<T>();
            foreach (T _object in _objects )
            {
                bool isAdded = _model.AddObject(_object);
                if (isAdded)
                    AddedObjects.Add(_object);
                AreAdded = AreAdded || isAdded;
            }
            _objects.Clear();

            foreach (T _addedObject in AddedObjects )
                _objects.Add(_addedObject);
            return AreAdded;
        }
        public override bool  UnExecute()
        {
            bool IsAdded = true;
            foreach (T _object in _objects)
                IsAdded = IsAdded && _model.RemoveObject(_object);
            return IsAdded;
        }

        public override string Description
        {
            get
            {
                return "Object(s) added to Models"; 
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }
    }
}
